﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace ActiveSharp.Utils
{
    public static class MemberInfoUtil
    {
        // from http://blogs.msdn.com/b/kingces/archive/2005/08/17/452774.aspx
        public static bool AreEqual(MemberInfo lhs, MemberInfo rhs)
        {
            if (lhs == rhs)
                return true;


            if (lhs.DeclaringType != rhs.DeclaringType)
                return false;


            // Methods on arrays do not have metadata tokens but their ReflectedType
            // always equals their DeclaringType
            if (lhs.DeclaringType != null && lhs.DeclaringType.IsArray)
                return false;


            if (lhs.MetadataToken != rhs.MetadataToken || lhs.Module != rhs.Module)
                return false;

            if (lhs is MethodInfo)
            {
                MethodInfo lhsMethod = lhs as MethodInfo;

                if (lhsMethod.IsGenericMethod)
                {
                    MethodInfo rhsMethod = rhs as MethodInfo;

                    Type[] lhsGenArgs = lhsMethod.GetGenericArguments();
                    Type[] rhsGenArgs = rhsMethod.GetGenericArguments();
                    for (int i = 0; i < rhsGenArgs.Length; i++)
                    {
                        if (lhsGenArgs[i] != rhsGenArgs[i])
                            return false;
                    }
                }
            }

            return true;

        }
    }
}
